package com.ck.ck_back.controller;

import com.ck.ck_back.pojo.Result;
import com.ck.ck_back.service.historyservice;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/history")
@RestController
public class HistoryController {
    @Resource
    private historyservice historyserviceimpl;

    //    增加历史记录，404为无此人，403无此作品，1成功，2失败
    @PostMapping("/add")
    public Object add(String openid,int production_id){
        int result = historyserviceimpl.addhistory(openid,production_id);
        if (result == 1){
            return Result.success();
        }else if (result == 2){
            return Result.failure("插入失败");
        }else if (result == 403){
            return Result.failure("无此作品");
        }else {
            return Result.failure("非法人员");
        }
    }

    //    删除历史（将state修改为0），404，参数错误，3无此历史，2删除错误，1成功
    @PostMapping("/delete")
    public Object delete(int historyId){
        int result = historyserviceimpl.deletehistory(historyId);
        if (result == 1){
            return Result.success();
        }else if (result == 2){
            return Result.failure("删除失败");
        }else if (result == 3){
            return Result.failure("无此历史");
        }else {
            return Result.failure("参数错误");
        }
    }

    @PostMapping("/getnum")
    public Object getnum(String openid,int production_id){
        return Result.success(historyserviceimpl.get_history_num(openid,production_id));
    }

    @PostMapping("/gethistory")
    public Object gethistory(String openid){

        System.out.println(openid);
        return Result.success(historyserviceimpl.getall(openid));
    }

}
